/**
 * Created by wei on 17/12/6.
 */

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import PArticle from './particle';
import Pager from '../common/pager';
import base from '../base.js';
import axios from 'axios';
import DivShow from '../front/divshow';

const basePath = base().basePath;


class DataTables extends  Component{

    constructor(props){
        super(props);
        this.state = {
            dataSource: [],
            pageInfo:{},
            type:'follow',
            token:'',
            tipVisible: false,
            keyword:'',
            delId:"",
        }
    }

    componentWillReceiveProps(nextProps){
        const {dataSource, pageInfo, type, token} = nextProps;
        this.setState({
            dataSource: dataSource,
            pageInfo: pageInfo,
            type: type,
            token: token,
        });
    }

    closeDeleteTip(){
        this.setState({
            tipVisible: false,
        });
    }

    handlePageChanged(num){
        var url = "";
        switch (this.state.type){
            case 'follow':
                url = basePath+"/topic/follow";
                break;
            case 'topic':
                url = basePath+"/topic/t";
                break;
            case 'replay':
                url = basePath+"/replay/r";
                break;
            case 'comment':
                url = basePath+"/comment/c";
                break;

        }
        this.state.pageInfo.pageNumber = num;
        axios.post(url , this.state.pageInfo, {headers: { token: this.state.token }
        })
            .then((res) => {
                if(res.status == 200){
                    var pageInfo = {};
                    var dataSource = res.data.content;
                    pageInfo.totalPages = res.data.totalPages;
                    pageInfo.totalElements = res.data.totalElements;
                    pageInfo.last = res.data.last;
                    pageInfo.first = res.data.first;
                    pageInfo.size = res.data.size;
                    pageInfo.pageSize = res.data.size;
                    pageInfo.pageNumber = res.data.number;
                    this.setState({
                        dataSource: dataSource,
                        pageInfo: pageInfo,
                        tipVisible: false,
                    });
                }
            });
    }

    refresh(){
        this.handlePageChanged(this.state.pageInfo.pageNumber);
    }

    fnShowTopic(id){
        this.setState({
            tipVisible: true,
            keyword: "发表",
            delId: id,
        });
    }

    fnShowReplay(id){
        this.setState({
            tipVisible: true,
            keyword: "回复",
            delId: id,
        });
    }

    fnShowComment(id){
        this.setState({
            tipVisible: true,
            keyword: "评论",
            delId: id,
        });
    }

    delete(){
        switch (this.state.type){
            case 'topic':
                this.delTopic(this.state.delId);
                break;
            case 'replay':
                this.delReplay(this.state.delId);
                break;
            case 'comment':
                this.delComment(this.state.delId);
                break;
        }
    }

    delTopic(id){
        axios.post(basePath+"/topic/del" , {topicId: id}, {headers: { token: this.state.token }
        })
            .then((res) => {
                if(res.status == 200){
                    this.refresh();
                }
            });
    }

    delReplay(id){
        axios.post(basePath+"/replay/del" , {replayId: id}, {headers: { token: this.state.token }
        })
            .then((res) => {
                if(res.status == 200){
                    this.refresh();
                }
            });
    }

    delComment(id){
        axios.post(basePath+"/comment/del" , {commentId: id}, {headers: { token: this.state.token }
        })
            .then((res) => {
                if(res.status == 200){
                    this.refresh();
                }
            });
    }

    render(){

        let ars;
        var that = this;
        ars = this.state.dataSource.map(function (v, i, arr) {
            return <PArticle fnDelTopic={that.fnShowTopic.bind(that)} fnDelReplay={that.fnShowReplay.bind(that)} fnDelComment={that.fnShowComment.bind(that)} type={that.state.type} topic={v} replays={v.replays} userInfo={v.user} />
        })

        return (
            <div>
                {ars}
                <div style={{width:'65%',margin:'0 auto'}}>
                <Pager pageChange={this.handlePageChanged.bind(this)} pageInfo={this.state.pageInfo}/>
                </div>
                <DivShow close={this.closeDeleteTip.bind(this)} visible={this.state.tipVisible}>
                    <div style={{height:'100px'}}>
                        <div style={{height:'60px'}}>
                            一旦删除将无法恢复,您确定删除该[{this.state.keyword}]内容吗?
                        </div>
                        <div>
                            <a onClick={this.closeDeleteTip.bind(this)} class="btn1 float_r" href="javascript:;">取消</a>
                            <a onClick={this.delete.bind(this)} class="btn1 float_r" href="javascript:;">确定</a>
                        </div>
                    </div>
                </DivShow>
            </div>
        );
    }

}

DataTables.propTypes = {
    dataSource: PropTypes.array.isRequired,
    pageInfo: PropTypes.object.isRequired,
    type: PropTypes.string.isRequired,
    token: PropTypes.string.isRequired,
};

export default DataTables;